<?php
/**
 * Class WechatMiniPush
 * Desc 小程序推送消息
 * Create on 2021/10/20 17:19
 * Create by wangyafang
 */

namespace app\api\controller\xyb;

class WechatMiniPush extends Base
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    public function PushOneMsg($pushInfo)
    {
        $appid =$this->configObj->app_id;//小程序appid
        $appsecret =$this->configObj->app_secret;//小程序秘钥
        $access_token = $this->mini_get_access_token($appid,$appsecret);

        $url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token={$access_token}";
        $openid = $pushInfo['openid'];
        $data = [
            'access_token' => $access_token,
            'touser' => $openid,
            'template_id' => $pushInfo['template_id'],
            'page' => $pushInfo['page'],
            'form_id' => $pushInfo['form_id'],
            'data' => $this->pushKeywords($pushInfo),
        ];
        return $this->http_post_data($url, $data);
    }

    public function PushPublicMsg($pushInfo)
    {
        $access_token = (new Wechat())->_getAccessToken();

        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$access_token}";
        $openid = $pushInfo['openid'];

        $data = [
            'touser' => $openid,
            'template_id' => $pushInfo['template_id'],
            'data' => $this->pushKeywords($pushInfo),
            'miniprogram'=>$pushInfo['miniprogram'],
        ];



        return $this->http_post_data($url, $data);
    }


    public function pushKeywords($pushInfo)
    {
//        $data = [
//            'character_string5' => ['value' => $pushInfo['character_string5'],],
//            'thing1' => ['value' => $pushInfo['thing1']],
//            'name3' => ['value' => $pushInfo['name3']],
//            'phone_number4' => ['value' => $pushInfo['phone_number4']],
//            'time6' => ['value' => $pushInfo['time6']],
//        ];
        return $pushInfo['data'];
    }

    /**http - post请求*/
   private function http_post_data($url, $data)
    {
        $data = json_encode($data);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_HTTPHEADER,array(
            'Content-Type: application/json; charset=utf-8',
            'Content-Length: ' . strlen($data)
        ));
        return curl_exec($ch);
    }


    /**发起网络请求 获取 access_token */
    private function mini_get_access_token($appId, $appSecret){
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appId}&secret={$appSecret}";
        $info = \fast\Http::get($url);
        if (empty($info)) {
            return '';
        } else {
            $json = json_decode($info);
            if (empty($json)) {
                return '';
            } else {
                $arr = get_object_vars($json);
                return $arr['access_token'];
            }
        }
    }

    public function template_id_list()
    {
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }

        $data = [
            'receive_order_success'=>$this->configObj->receive_order_success,
            'order_complete'=>$this->configObj->order_complete,
            'delivery_complete'=>$this->configObj->delivery_complete,
            'new_order_notification'=>$this->configObj->new_order_notification,
            'application_status_notification'=>$this->configObj->application_status_notification,
            'audit_result_notification'=>$this->configObj->audit_result_notification,
        ];
        $this->success('请求成功',$data);
    }



}